<?php 
echo "<h2>tcp/ip connection</h2>\n";
$server_port = 10005;
$address = "127.0.0.1";

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket === false){
      echo "socket_create() failed reason:".socket_strerror(socket_last_error())."\n";
}else{
      echo "OK. \n";
}

echo "Attmemting to connect to '$address' on port '$server_port'...";
$result = socket_connect($socket,$address,$server_port);
if($result === false){
      echo "socket_connect() failed.\n Reason: ($result) ".socket_strerror(socket_last_error($socket))."\n";
}else{
      echo "OK. \n";
}
$in = "HEAD / http/1.1\r\n";
$in = "HOST; localhost \r\n";
$in = "Connection: close\r\n\r\n";
$out = "";
echo "sending http head request...";
socket_write($socket,$in,strlen($in));
echo "OK \n";

echo "Reading response:\n\n";
while($out = socket_read($socket,8192)){
      echo $out;
}
echo "closeing socket...";
socket_close($socket);
echo "ok .\n\n";
